
package com.jair.web.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jair.common.bean.EasyUIResult;
import com.jair.common.service.ApiService;
import com.jair.manage.pojo.Content;

/**
 * @Description 
 * @FullName com.jair.web.service.IndexService 
 * @Author Jair_Huang
 * @Create 2017年6月1日 下午12:59:35   
 */
@Service
public class IndexService {
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexService.class);
    @Value("${MANAGE_JAIR_URL}")
    private String MANAGE_JAIR_URL;

    @Value("${INDEX_AD1_URL}")
    private String INDEX_AD1_URL;

    @Autowired
    private ApiService apiService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //    public String getIndexAd1() {
    //        // 通过调用后台系统系统的接口获取数据
    //        String url = MANAGE_TAOTAO_URL + INDEX_AD1_URL;
    //        try {
    //            String jsonData = this.apiService.doGet(url);
    //            // 解析json，获取需要的数据
    //            // 封装成前端所需的格式
    //
    //            JsonNode jsonNode = MAPPER.readTree(jsonData);
    //            ArrayNode arrayNode = (ArrayNode) jsonNode.get("rows");
    //
    //            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    //
    //            for (JsonNode node : arrayNode) {
    //                Map<String, Object> map = new LinkedHashMap<String, Object>();
    //                map.put("srcB", node.get("pic").asText());
    //                map.put("height", 240);
    //                map.put("alt", node.get("title").asText());
    //                map.put("width", 670);
    //                map.put("src", map.get("srcB"));
    //                map.put("widthB", 550);
    //                map.put("href", node.get("url").asText());
    //                map.put("heightB", 240);
    //
    //                result.add(map);
    //            }
    //            //将java对象序列化成json
    //            return MAPPER.writeValueAsString(result);
    //
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    //        return null;
    //    }

    @SuppressWarnings("unchecked")
    public String getIndexAd1() {
        // 通过调用后台系统系统的接口获取数据
        String url = MANAGE_JAIR_URL + INDEX_AD1_URL;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("请求路径！ url = {}", url);
        }
        try {
            String jsonData = this.apiService.doGet(url);
            // 解析json，获取需要的数据
            // 封装成前端所需的格式

            EasyUIResult easyUIResult = EasyUIResult.formatToList(jsonData, Content.class);
            List<Content> contents = (List<Content>) easyUIResult.getRows();
            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

            for (Content content : contents) {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put("srcB", content.getPic());
                map.put("height", 240);
                map.put("alt", content.getTitle());
                map.put("width", 670);
                map.put("src", content.getPic());
                map.put("widthB", 550);
                map.put("href", content.getUrl());
                map.put("heightB", 240);

                result.add(map);
            }
            // 将java对象序列化成json
            return MAPPER.writeValueAsString(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
